***************************************************************************
*** Rural_5g.do
***************************************************************************

use "Raw Data/sp2015.dta",clear

gen rural_countycode=substr(ID,6,2)
gen rural_towncode=substr(ID,6,4)

gen stratification="S1_"+rural_countycode

ren disease case
rename level level_sub
rename r_qe_pct r_qep
rename e_qe_pct e_qep
rename consultation_fee totalfee
rename corr_part_mana cpmana
ren provdiag diag
ren drug_prescribe medi
ren medi_num_cond nummedi_cond
ren correct_referral_cond corrrefer_cond

gen corrdiag = 1 if corrdiag_cond == 1
replace corrdiag = 0 if corrdiag_cond == 0 | diag == 0

gen pcorrdiag = 1 if pcorrdiag_cond == 1
replace pcorrdiag = 0 if pcorrdiag_cond == 0 | diag == 0

gen cpcorrdiag = 1 if corrdiag == 1 | pcorrdiag == 1
replace cpcorrdiag = 0 if corrdiag == 0 & pcorrdiag == 0

ren corrmedi_cond corrmedi_condd
gen corrmedi_cond = 1 if corrmedi_condd == 1
replace corrmedi_cond = 0 if corrmedi_condd == 0 | (case == "T" & medi == 1)

generate level = "rural" if level_sub == "Township" | level_sub == "Village"


label variable level "rural/urban/online"

** missing value of Angina - County - Referred
replace corrrefer_cond=1 if (case == "A" | case == "T") & refer == 1
replace corrrefer_cond=0 if case == "D" & refer == 1

keep ID case level level_sub r_qep e_qep totalfee diag corrdiag_cond pcorrdiag_cond corrmana pcorrmana cpmana ///
corrdiag pcorrdiag cpcorrdiag ///
medi nummedi_cond corrmedi_cond antibiotic_cond refer corrrefer_cond stratification

save "working data/SPrural.dta", replace

*************************************************************************************
*** Rural_merge3.do
*************************************************************************************

use "${rawdata}/sp2015_physician_character.dta", clear

ren pracdoc pcertificate
ren assipracdoc apcertificate
ren ruraldoc rcertificate

gen age1 = 0
replace age1 = 1 if age_level == 1 | age < 30

ta age age1, m
ta age_level age1, m
 
gen age2 = 0
replace age2 = 1 if (age >= 30 & age <40) | age_level == 2 
                    
gen age3 = 0
replace age3 = 1 if (age >= 40 & age <50) | age_level == 3

gen age4 = 0
replace age4 = 1 if (age >= 50 & age <60) | age_level == 4

gen age5 = 0
replace age5 = 1 if (age >= 60 & age != .) | age_level == 5 

replace age = 25 if age_level == 1
replace age = 35 if age_level == 2
replace age = 45 if age_level == 3
replace age = 55 if age_level == 4 


replace pcertificate = 0 if pcertificate != 1
replace apcertificate = 0 if apcertificate != 1
replace rcertificate = 0 if rcertificate != 1


gen othertitle = 1 if (pcertificate == 0 & apcertificate == 0 & rcertificate == 0)  | CH == 1
replace othertitle = 0 if  pcertificate == 1 | apcertificate == 1 | rcertificate == 1 


keep ID age age1 age2 age3 age4 age5 male pcertificate apcertificate rcertificate othertitle 

save "$workingdata/rural_physician.dta", replace



use "$workingdata/SPrural.dta", clear

merge 1:1 ID using "$workingdata/rural_physician.dta"
drop if _merge== 2
drop _merge

save "$workingdata/SPruralmerge.dta", replace

*********************************************************************************
*** Urban2017.do
*********************************************************************************
use "$rawdata/urban2017.dta"

gen stratification="S2_"+COUNTY // or,HEALTHCE

drop case

gen case = "A" if case_01 == 0
replace case = "As" if case_01 == 1

keep if case == "A"

generate level = "urban"
gen level_sub = "CHC"
generate level_sub1 = "CHC"


ren gendernew male
ren QCODE_01 ID 
tostring ID, replace
gen r_qep = recommondexamspercentage/100
gen e_qep = essentiaexamspercentage/100
ren totalcost totalfee
ren diagnosis diag

gen corrdiag = 1 if dianosisoutcomefinal == 1
replace corrdiag = 0 if dianosisoutcomefinal != 1

gen pcorrdiag = 1 if diagnosisoutcomes == 2
replace pcorrdiag = 0 if diagnosisoutcomes != 2

gen cpcorrdiag = 1 if diagnosisoutcomes == 2 | diagnosisoutcomes == 1
replace cpcorrdiag = 0 if diagnosisoutcomes != 2 & diagnosisoutcomes != 1



ren correcttreatmentnew corrmana
ren medicineornot medi
ren numberofmedicine_02 nummedi_cond
ren refarral refer

gen corrrefer_cond = 1 if case == "A" & refer == 1


gen pcorrmana = 0
gen cpmana = corrmana
gen antibiotic_cond = .

gen corrmedi_cond = 1 if correctmedication == 1
replace corrmedi_cond = 0 if ( correctmedication == 2 ) | (case == "T" & medi == 1)


drop age
gen age = .
replace age = 25 if AGE == 1  
replace age = 35 if AGE == 2
replace age = 45 if AGE == 3
replace age = 55 if AGE == 4 

gen age1 = 0
replace age1 = 1 if AGE == 1  
 
gen age2 = 0
replace age2 = 1 if AGE == 2
                    
gen age3 = 0
replace age3 = 1 if AGE == 3 

gen age4 = 0
replace age4 = 1 if AGE == 4  

gen age5 = 0


gen pcertificate = 1 if CERTIFIC == 3
replace pcertificate = 0 if CERTIFIC != 3

gen apcertificate = 1 if CERTIFIC == 2
replace apcertificate = 0 if CERTIFIC != 2

gen othertitle = 1 if pcertificate == 0 & apcertificate == 0 
replace othertitle = 0 if pcertificate == 1 | apcertificate == 1 

keep ID case stratification age age1 age2 age3 age4 age5 male pcertificate apcertificate  ///
 othertitle ///
level level_sub level_sub1 r_qep e_qep totalfee diag corrdiag pcorrdiag cpcorrdiag corrmana pcorrmana cpmana ///
medi nummedi_cond corrmedi_cond antibiotic_cond refer corrrefer_cond

save "working data/urban2017.dta", replace

*******************************************************************************
*** Migrant3.do
*******************************************************************************
use "$rawdata/MVC.dta",clear

gen stratification="S3_"+string(villagecode) // or,towncode

drop if vignette == 1

drop level
gen level = "MVC"
rename disease case

gen level_sub = "MVC"
gen level_sub1 = "MVC"


rename arqe r_qep
rename aeqe e_qep
rename totfee totalfee
ren gavediag diag
ren partcorrtreat pcorrmana
ren corrtreat corrmana
ren pcorrtreat cpmana
ren drugpres medi
ren numofdrug nummedi_cond
ren antibiotic antibiotic_cond
ren referral refer

ren pracdoc pcertificate
ren assipracdoc apcertificate
ren ruraldoc rcertificate


gen age1 = 0
replace age1 = 1 if age < 30  
 
gen age2 = 0
replace age2 = 1 if age >= 30 & age <40 
                    
gen age3 = 0
replace age3 = 1 if age >= 40 & age <50

gen age4 = 0
replace age4 = 1 if age >= 50 & age <60

gen age5 = 0
replace age5 = 1 if age >= 60 & age <99


gen corrmedi_cond = 1 if pcorrdrug == 1
replace corrmedi_cond = 0 if ( pcorrdrug == 0 & medi == 1 ) | (case == "T" & medi == 1)

gen corrdiag_cond = 1 if corrdiag == 1
replace corrdiag_cond = 0 if corrdiag == 0 & diag == 1

gen pcorrdiag_cond = 1 if pcorrdiag == 1
replace pcorrdiag_cond = 0 if pcorrdiag == 0 & diag == 1

gen cpcorrdiag = 1 if corrdiag == 1 | pcorrdiag == 1
replace cpcorrdiag = 0 if corrdiag == 0 & pcorrdiag == 0

replace nummedi_cond = . if medi == 0
replace antibiotic_cond  = . if medi == 0


gen corrrefer_cond = 1 if case != "D" & refer == 1
replace corrrefer_cond = 0 if case == "D" & refer == 1

gen othertitle = 1 if certdoc != 1
replace othertitle = 0 if pcertificate == 1 | apcertificate == 1 | rcertificate == 1  

keep ID case stratification age age1 age2 age3 age4 age5 male pcertificate apcertificate rcertificate othertitle ///
level level_sub level_sub1 r_qep e_qep totalfee diag corrdiag_cond pcorrdiag_cond corrmana pcorrmana cpmana ///
corrdiag pcorrdiag cpcorrdiag ///
medi nummedi_cond corrmedi_cond antibiotic_cond refer corrrefer_cond

save "$workingdata/MVC.dta", replace

****************************************************************************************
*** Online3.do
****************************************************************************************
use "$rawdata/dtc_casemanage.dta", clear

generate level = "online"
generate level_sub = "online"
generate level_sub1 = "online"

ren consultation_fee totalfee
rename interview_id ID
ren r_qe_pct r_qep
ren e_qe_pct e_qep
ren corr_part_mana cpmana
ren provdiag diag
ren drug_prescribe medi
ren medi_num_cond nummedi_cond


gen pcertificate = 1 if doctor_title1 == 1 | doctor_title2 == 1 | doctor_title3 == 1 | doctor_title4 == 1
replace pcertificate = 0 if doctor_title1 == 0 & doctor_title2 == 0 & doctor_title3 == 0 & doctor_title4 == 0

gen othertitle = 0


drop age
gen age = .
replace age = 25 if age1 == 1  
replace age = 35 if age2 == 1
replace age = 45 if age3 == 1 
replace age = 55 if age4 == 1  
replace age = 65 if age5 == 1  



keep if case_type_combine ==  1 | case_type_combine == 7 | case_type_combine == 0 /*case0_version is viral diarrhea*/
generate case = "A" if case_type_combine == 1
replace case = "T" if case_type_combine == 7
replace case = "D" if case_type_combine == 0

gen cpcorrdiag = 1 if corrdiag == 1 | pcorrdiag == 1
replace cpcorrdiag = 0 if corrdiag == 0 & pcorrdiag == 0


gen corrrefer_cond = 1 if (case == "A" | case == "T")  & refer == 1
replace corrrefer_cond = 0 if case == "D" & refer == 1

gen stratification="S4_"+string(platform_code)

keep ID stratification age age1 age2 age3 age4 age5 male pcertificate othertitle ///
level level_sub level_sub1 r_qep e_qep totalfee diag corrdiag_cond pcorrdiag_cond corrmana pcorrmana cpmana ///
corrdiag pcorrdiag cpcorrdiag ///
medi nummedi_cond corrmedi_cond antibiotic_cond refer corrrefer_cond case


save "$workingdata/SPonline.dta", replace

************************************************************************
*** Append
************************************************************************
use "$workingdata/SPruralmerge.dta", clear

append using "$workingdata/SPonline.dta" "$workingdata/MVC.dta" "$workingdata/urban2017.dta"

replace pcertificate = 0 if pcertificate!=1 
replace apcertificate = 0 if apcertificate !=1
replace rcertificate = 0 if rcertificate !=1
replace othertitle = 1 if pcertificate == 0 & apcertificate == 0 & rcertificate == 0

gen certtotal=pcertificate+apcertificate+rcertificate+othertitle
tab certtotal, missing // verify that every observation has one and only one title
drop certtotal

gen rural = 1 if level == "rural"
replace rural = 0 if level != "rural"

gen MVC = 1 if level_sub == "MVC"
replace MVC = 0 if level_sub != "MVC"

gen county = 1 if level_sub == "County"
replace county = 0 if level_sub != "County"

gen CHC = 1 if level_sub == "CHC"
replace CHC = 0 if level_sub != "CHC"

gen online = 1 if level == "online"
replace online = 0 if level != "online"


gen A = 1 if case == "A"
replace A = 0 if case != "A"

gen D = 1 if case == "D" 
replace D = 0 if case != "D"

gen T = 1 if case == "T"
replace T = 0 if case != "T"

gen As = 1 if case == "As"
replace As = 0 if case != "As"

gen agenew1 = 1 if age1 == 1 | age2 ==1 |  age3 == 1
replace agenew1 = 0 if  age4 ==1 | age5 == 1

gen agenew2 = 1 if age4 == 1 | age5 ==1
replace agenew2 = 0 if age3 == 1 | age1 ==1 | age2 == 1

gen corrmedi = 1 if corrmedi_cond == 1
replace corrmedi = 0 if corrmedi_cond != 1 & medi != .

gen antibiotic = 1 if antibiotic_cond == 1
replace antibiotic = 0 if antibiotic_cond != 1 & medi != .

gen corrrefer = 1 if (case == "A" | case == "T") & refer == 1
replace corrrefer = 0 if corrrefer != 1

gen year = 2015 if level == "rural" | level_sub == "MVC" | level_sub == "County"
replace year = 2017 if level_sub == "CHC"
replace year = 2019 if level == "online"


save "$workingdata/append.dta", replace


